package com.infonautics.dao.imp;

import java.util.List;

import com.hibernate.HibernateDao;
import com.hibernate.Unit;
import com.infonautics.dao.IUnitDao;
import com.infonautics.page.PageBean;
import com.system.dao.ISystemDao;

/**
 * 
 * @author 马萍
 * 部门信息dao实现
 */
public class IUnitDaoImp implements ISystemDao<Unit>,IUnitDao{
	
	HibernateDao<Unit> unitDao = new HibernateDao<Unit>();
	
	/* 
	 * (non-Javadoc)
	 * @see com.system.dao.ISystemDao#doList(int, int, int)
	 */
	@Override
	public PageBean<Unit> doList(int pageSize, int page, int id)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * 添加部门信息
	 * (non-Javadoc)
	 * @see com.system.dao.ISystemDao#doAdd(java.lang.Object)
	 */
	@Override
	public void doAdd(Unit object) throws Exception {
		
		if(object != null){
			
			unitDao.addObject(object);
		}
	}

	/*
	 * 修改部门信息
	 * (non-Javadoc)
	 * @see com.system.dao.ISystemDao#doModfiy(java.lang.Object)
	 */
	@Override
	public void doModfiy(Unit object) throws Exception {
		
		if(object != null){
			
			unitDao.updateObject(object);
		}
	}

	/*
	 * 删除部门信息
	 * (non-Javadoc)
	 * @see com.system.dao.ISystemDao#doDelete(int)
	 */
	@Override
	public void doDelete(int id) throws Exception {
		
		unitDao.deleteObject(id, Unit.class);
	}

	/*
	 * 根据id查询
	 * (non-Javadoc)
	 * @see com.system.dao.ISystemDao#doOne(int)
	 */
	@Override
	public Unit doOne(int id) throws Exception {
		
		return unitDao.getObject(id, Unit.class);
	}

	/*
	 * 全部查询部门信息
	 * (non-Javadoc)
	 * @see com.infonautics.dao.IUnitDao#doUnitList(int, int)
	 */
	@Override
	public PageBean<Unit> doUnitList(int pageSize, int page)
			throws Exception {
		
		final String hql = "from Unit order by id desc";
		int allRow = unitDao.getAllRowCount("select count(*) from Unit");
		
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int length = pageSize;
		final int currentPage = PageBean.countCurrentPage(page);
		List<Unit> list = unitDao.getObjects(hql, offset,length);
		
		PageBean<Unit> pageBean = new PageBean<Unit>();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean;
	}

	/*
	 * 条件查询部门信息
	 * (non-Javadoc)
	 * @see com.infonautics.dao.IUnitDao#doQueryList(int, int, java.lang.String)
	 */
	@Override
	public PageBean<Unit> doQueryList(int pageSize, int page, String sql)
			throws Exception {
		
		final String hql = "from Unit where 1=1 "+sql+" order by id desc";
		int allRow = unitDao.getAllRowCount("select count(*) from Unit where 1=1 "+sql);
		
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int length = pageSize;
		final int currentPage = PageBean.countCurrentPage(page);
		List<Unit> list = unitDao.getObjects(hql, offset,length);
		
		PageBean<Unit> pageBean = new PageBean<Unit>();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean;
	}

}
